const ws = require("nodejs-websocket");
const {getPort} = require("./portIsOccupied");
const {IPv4Address} = require('../util/ip4address');

console.log("准备websocket...");

var server = ws.createServer(function (conn) {
  conn.on("text", function (str) {
    console.log("收到的信息为:" + str);
    conn.sendText('回复了' + str)
  });
  conn.on("close", function (code, reason) {
    console.log("关闭连接");
  });
  conn.on("error", function (code, reason) {
    console.log("异常关闭")
  });
});

getPort().then((port) => {
  global.wsPort = port;
  server.listen(port);
  console.log("WebSocket建立完毕 start at ws://" + IPv4Address + ":" + port);
}).catch((e) => {
  console.error('&&&&&&&&&&&&&&&&', e)
});

